package com.wanmait.happyheating.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.pojo.FaultType;
import com.wanmait.happyheating.service.FaultTypeService;
import com.wanmait.happyheating.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author yuQianTao
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/manage")
public class AdminFaultTypeController {

    @Resource
    private FaultTypeService faultTypeService;

    /**
     *验证故障类型是否重复
     * @author yuQianTao
     * @since 2021-05-17
     */
    @PostMapping("findFaultType")
    public Result findFaultType(@RequestBody FaultType faultType)
    {
        LambdaQueryWrapper<FaultType> faultTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        faultTypeLambdaQueryWrapper.eq(FaultType::getTitle,faultType.getTitle());
        List<FaultType> faultTypes=faultTypeService.list(faultTypeLambdaQueryWrapper);
        if (faultTypes.size()>0)
        {
            return  Result.error("有重复！");
        }
        return Result.success("无重复");
    }
    /**
     *查找显示所有的故障类型
     * @author yuQianTao
     * @since 2021-05-17
     */
    @PostMapping("showFaultType")
    public Result showFaultType(@RequestBody FaultType faultType, Integer pageNum,Integer pageSize)
    {
        if (pageNum==null){
            pageNum=1;
        }

        if (faultType.getId()==0&&!("".equals(faultType.getTitle()))){
            PageInfo<FaultType> faultTypes=faultTypeService.likeFaultTypeByPage(pageNum,pageSize,faultType.getTitle());
            return Result.success("查找成功",faultTypes);
        }
        PageInfo<FaultType> faultTypes=faultTypeService.findFaultTypeByPage(pageNum,pageSize);
        return Result.success("查找成功",faultTypes);
    }

    /**
     *增加故障类型
     * 备注：先经过网页的查重和Ajax验证后到达此功能
     * @author yuQianTao
     * @since 2021-05-17
     */
    @PutMapping("addFaultType")
    public Result addFaultType(@RequestBody FaultType faultType)
    {
        Boolean flag=faultTypeService.save(faultType);
        if(flag){
            return Result.success("添加完成");
        }
        else {return Result.error("添加失败");}
    }

    /**
     *模糊查询
     *
     * @author yuQianTao
     * @since 2021-05-17
     */
    @PostMapping("likeFaultType")
    public Result likeFaultType(@RequestBody FaultType faultType)
    {
        LambdaQueryWrapper<FaultType> faultTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        faultTypeLambdaQueryWrapper.like(FaultType::getTitle,faultType.getTitle());
        List<FaultType> faultTypes=faultTypeService.list(faultTypeLambdaQueryWrapper);
        return Result.success("查询完成",faultTypes);
    }

    /**
     *修改
     *
     * @author yuQianTao
     * @since 2021-05-17
     */
    @PostMapping("updateFaultType")
    public Result updateFaultType(@RequestBody FaultType faultType)
    {
        if (faultType.getId()!=null)
        {
            boolean flag=faultTypeService.updateById(faultType);
            if (flag){
                return Result.success("更新完成");
            }else {
                return Result.error("更新失败");
            }
        }
        else {
            return Result.error("无效的id");
        }
    }

    /**
     *删除
     *
     * @author yuQianTao
     * @since 2021-05-17
     */
    @DeleteMapping("deleteFaultType{id:\\d+}")
    public Result deleteFaultType(@PathVariable Integer id)
    {
        if (id!=null)
        {
            boolean flag=faultTypeService.removeById(id);
            if (flag){
                return Result.success("删除完成");
            }else {
                return Result.error("删除失败");
            }
        }
        else {
            return Result.error("无效的id");
        }
    }
}
